$(function(){
    obj.init();
});

var obj = new JsFun();
function JsFun() {
    return {
        dataGrid: null,
        dom: {
            search: null,
            startTime: null,
            endTime: null,
            subject: null,
            schoolName: null
        },
        params: {
            appCode: null,
            startTime: null,
            endTime: null
        },
        init: function () {
            this.params.appCode     = xhTool.getQuery('iAppCode');
            this.params.startTime   = xhTool.getQuery('iStartTime') || xhTool.getNowDate(-7);
            this.params.endTime     = xhTool.getQuery('iEndTime') || xhTool.getNowDate(-1);

            this.dom.search     = $('#bt_list_search');
            this.dom.startTime  = $('#list_start_time');
            this.dom.endTime    = $('#list_end_time');
            this.dom.subject    = $('#list_subject');
            this.dom.schoolName = $('#list_school_name');

            this.dom.startTime.datebox('setValue', this.params.startTime);
            this.dom.endTime.datebox('setValue', this.params.endTime);

            this.dataGrid = new xhDataGrid($('#list'), 'Stats/getSubjectOverviewData', {
                appCode: this.params.appCode,
                startTime: this.params.startTime,
                endTime: this.params.endTime
            });
            this.dataGrid.init();
            this.funObj().bindDataGridEvent();

            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            $this.dom.search.click(function () {
                $this.funObj().search();
            });
            xhTool.bindDocKeyDown($this.dom.search);
        },
        funObj: function() {
            var $this = this;
            return {
                bindDataGridEvent: function () {
                    $this.dataGrid.onLoadSuccess = function (data) {
                        if (data.rows.length <= 0) return false;
                        $this.dataGrid.mergeCells(['sSchoolName']);
                    };
                    $this.dataGrid.onDblClickCell = function(rowIndex, field, value){
                        if (field !== 'iTaskFixNum') {
                            return false;
                        }
                        var data = $this.dataGrid.list.datagrid('getRows');
                        data = data[rowIndex];
                        window.top.xhTop.createNewTab('统计_教师布置_' + data.sTchName, baseUrl + 'Stats/classTaskInfo?' + xhTool.parseParam({
                            iAppCode: $this.params.appCode,
                            iSchoolId: data.iSchoolId,
                            iStartTime: xhExtra.getValue($this.dom.startTime),
                            iEndTime: xhExtra.getValue($this.dom.endTime),
                            iTchId: data.iTchId
                        }));
                    };
                },
                search: function () {
                    var params = {
                        startTime: xhExtra.getValue($this.dom.startTime),
                        endTime: xhExtra.getValue($this.dom.endTime),
                        subject: xhExtra.getValue($this.dom.subject),
                        schoolName: xhExtra.getValue($this.dom.schoolName)
                    };
                    if (!params.startTime || !params.endTime) {
                        window.top.xhTool.showError('时间不能为空！');
                        return false;
                    }
                    $this.dataGrid.reload(params);
                }
            }
        }
    }
}

// $(document).ready(function(){
//     var appCode   = xhTool.getQuery('iAppCode');
//     var list      = $('#List');
//     var startDom  = $('#List_Search_StartTime');
//     var endDom    = $('#List_Search_EndTime');
//     var startTime = xhTool.getQuery('iStartTime');
//     var endTime   = xhTool.getQuery('iEndTime');
//     var schoolDom = $('#List_Search_SchoolName');
//     var subjectDom= $('#List_Search_Subject');
//     if(!startTime){
//         startDom.datebox('setValue', GetDateStr(-7));
//         startTime = startDom.getElementValue();
//     }
//     else{
//         startDom.datebox('setValue', startTime);
//     }
//     if(!endTime){
//         endDom.datebox('setValue', GetDateStr(-1));
//         endTime   = endDom.getElementValue();
//     }
//     else{
//         endDom.datebox('setValue', endTime);
//     }
//     var queryParams = {
//         iAppCode: appCode,
//         iStartTime: startTime,
//         iEndTime: endTime
//     };
//     list.initDataGrid(baseUrl + 'Stats/getSubjectOverviewData', {queryParams: queryParams});
//     DataGrid.onLoadSuccess = function(data){
//         if (data.rows.length <= 0) return false;
//         list.mergeCellsByField(['sSchoolName']);
//     };
//     var searchDom =  $('#bt_list_search');
//     searchDom.click(function(){
//         getParams();
//         list.addDataGridParams(queryParams);
//     });
//     xhTool.bindDocKeyDown(searchDom);
//
//     function getParams(){
//         startTime = startDom.getElementValue();
//         endTime   = endDom.getElementValue();
//         if(!startTime || !endTime){
//             alert('时间不可为空！');
//             return false;
//         }
//         queryParams.iStartTime = startTime;
//         queryParams.iEndTime   = endTime;
//         queryParams.sSchoolName= schoolDom.getElementValue();
//         queryParams.sSubject   = subjectDom.getElementValue();
//     }
// });
// function GetDateStr(AddDayCount) {
//     var dd = new Date();
//     dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
//     var y = dd.getFullYear();
//     var m = dd.getMonth()+1;//获取当前月份的日期
//     var d = dd.getDate();
//     return y+"-"+m+"-"+d;
// }